<?php

/** 
 * @author Star.Li
 * @copyright FZD
 * @update 2011-12-10
 */
class Model{
	//TODO - Insert your code here
	//表前缀
	protected $_prefix    = "m_";
	
	//用户等录信息
	protected $userInfo   = "";
	//表名
	protected  $_name     = "";
	//数据库对象
	protected $_db        = "";
	//cache 对象1
	public $_cache     = "";
	//sessionID
	public $_sessionId = "";
	//cache前缀
	protected $_cache_key = "";
	//是否过滤插入及更新数据内容
	public $is_filter     = true;
	public $_sql          = "";
	public $_command      = "";
	function __construct() {
		
		//TODO - Insert your code here
		$this->_sessionId = session_id();
		$this->_cache_key = $this->_sessionId.MEM_KEY;
		//用户信息
		//$this->userInfo       = Yii::app()->cache->get($this->_cache_key.'userInfo');
		//获取DB对象
		$this->_db      = Yii::app()->db;
		//获取Memcache对象
		//$this->_cache   = Yii::app()->cache;
		$this->init();
	}
	protected function init(){
		
	}
	function getBookDb(){
		return Yii::app()->bookdb;
	}
	function getVosDb(){
		return Yii::app()->vosdb;
	}
	function setDb($db){
		$this->_db = $db;
		return $this;
	}
	function setCache($cache){
		$this->_cache = $cache;
	}
	function setPrefix($name){
		$this->_prefix = $name;
		return $this;
	}
	function setName($name){
		$this->_name = $this->_prefix.$name;
		return $this;
	}
	
	private function filter($str){
		if($this->is_filter){
			return addslashes($str);
		}
		return $str;
	}
	
	function getName(){
		return $this->_name;
	}
	/**
	 * 执行SQL语句
	 * @param $sql
	 */
	function query($sql){
		$this->_sql = $sql;
		$this->_command = $this->_db->createCommand($sql);
		
		return $this;
	}
	/**
	 * 获取结果Array()
	 * Enter description here ...
	 */
	function fetchAll(){
		return $this->_command->queryAll();
	}
	/**
	 * 执行
	 */
	function execute(){
		return $this->_command->execute();
	}
	/**
	 * 获取单行结果
	 * Enter description here ...
	 */
	function fetchRow(){
		return $this->_command->queryRow();
	}
	/**
	 * 获取上次执行的SQL语句
	 * Enter description here ...
	 */
	function getSql(){
		return $this->_sql;
	}
	
	/**
	 * 
	 * 更新表数据
	 * @param $arr
	 * @param $wh
	 */
	function updateData($arr,$wh){
		$f = "";
		foreach($arr as $k=>$v){
			$v = trim($v);
			if($this->is_filter){
				$v = addslashes($v);
			}
			$f[] = "`".$k."`="."'".$v."'";
		}
		$sql = "update `".$this->_name."` set ".implode(',', $f)." where ".$wh;
		
		$this->_sql = $sql;
		return $this->query($sql)->execute();
	}
	/**
	 * 
	 * 插入表数据
	 * @param $arr
	 * @param $order
	 */
	function insertData($arr,$order = "id"){
		$f = $val = "";
		
		foreach($arr as $k=>$v){
			if($this->is_filter){
				$v = addslashes($v);
			}
			$f[] = "`".$k."`";
			$val[] = "'".$v."'";
		}
		$sql = "insert into `".$this->_name."` (".implode(',', $f).") values (".implode(',', $val).")";
		
		$this->_sql = $sql;
		
		$command = $this->_db->createCommand($sql);

		$rs = $command->execute();
		if($rs){
			return $this->_db->getLastInsertID();
		}
		return false;
	}
	/**
	 * 
	 * Enter description here ...
	 * @param $wh
	 */
	function getAll($wh=""){
		$sql = "select * from `".$this->_name."`";
		if(!empty($wh)){
			$sql .= " where ".$wh;
		}
		
		return $this->query($sql)->fetchAll();
	}
	/**
	 * 
	 * 获取列表
	 * @param string $wh
	 * @param string $order
	 * @param string $page
	 * @param string $url
	 * @param int $pageSize
	 */
	function getList($wh=null,$order=null,$page=1,$url=null,$pageSize=30){
		$sql = "select *  from `".$this->_name."`";
		
		if(!empty($wh)){
			$sql .= " where ".$wh;
		}
		
		$rs = $this->query($sql)->fetchAll();
		
		$total = count($rs);
		
		$pageCount = 0;
		
		if($total%$pageSize == 0){
			
			$pageCount = $total/$pageSize;
			
		}else{
			
			$pageCount = ceil($total/$pageSize);
			
		}
		
		$page = $page < 1 ? 1: $page;
		
		$s = $page * $pageSize - $pageSize;
		
		$s = $s < 0 ? '0':$s;
		
		if($order != null){
			$sql .= "order by ".$order;
		}
		$sql .= " limit $s,$pageSize ";
		
		$rs = $this->query($sql)->fetchAll();
		
		$data = array("data"=>$rs,"pageCount"=>$pageCount,"total"=>$total,"page"=>$page);
		
		if(!empty($url)){

			$page = new page($url, $pageCount, $total, $page);
			
			$data['assign'] = $page->assign();
		}
		
		return $data;
	}
	/**
	 * 获取总记录条数据
	 * @param String $wh
	 */
	function getCount($wh){
		$sql = "select count(*) as total from `".$this->_name."`";
		if(!empty($wh)){
			$sql .= " where ".$wh;
		}
		
		$rs = $this->query($sql)->fetchRow();
		if($rs['total']>0){
			return true;
		}
		return false;
	}
	function getById($wh){
		$sql = "select * from `".$this->_name."` ";
		if(!empty($wh)){
			$sql .= " where ".$wh;
		}
		return $this->query($sql)->fetchRow();
	}
	/**
	 * 
	 * 标记记录(不允许全部删除)
	 * @param String $wh
	 */
	function del($wh){
		if(empty($wh)){
			return false;
		}
		//$sql = "delete  from `".$this->_name."` where ".$wh;
		$sql = "update `".$this->_name."` set `deleted` = '1' where ".$wh;
		
		return $this->query($sql)->execute();
	}
	
	/**
	 * 
	 */
	function __destruct() {
	
		//TODO - Insert your code here
	}
}

?>